package com.sendinfo.xwork.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;

public abstract class CustomizeConverter extends DefaultTypeConverter {
	abstract protected Object fromString(String value, Class<?> clazz);

	@SuppressWarnings("unchecked")
	@Override
	public Object convertValue(Map context, Object value, Class clazz) {
		if (value instanceof String[]) {
			String[] params = (String[]) value;
			Object[] r = new Object[params.length];
			for (int i = 0; i < r.length; i++)
				r[i] = this.fromString(params[i], clazz);
			return r.length == 1 ? r[0] : r;
		} else if (value instanceof String) return this.fromString((String) value, clazz);
		else throw new RuntimeException("Can not convert non-string value.");
	}
}
